package com.ruoyi.generator.util;


import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;

import java.awt.*;
import java.util.List;
import java.util.UUID;


public class SpireDocUtils {


    //创建 SpireDocUtils 的一个对象
    private static SpireDocUtils instance = new SpireDocUtils();

    //让构造函数为 private，这样该类就不会被实例化
    private SpireDocUtils() {
        com.spire.license.LicenseProvider.setLicenseKey("WJiBW6ft0mIBtzoBAIX4fVWJVeRL4ASY6iAiH3CBSj6jig65KLn3Axa4XS/szZVdbPy5Nu1q5FcRGqrXqfmrmpY2gTCcY3JPz8HysDM8/YFva7Ntz8RNB2xyslU1J/dRSEwnSVxbhi6biEujNAUg7ugvlSpQEibdd83mxurSeuLT510fVvp+IXLxcFHmsLb7hput1KH5kwHnI9z4pftNfCwdfNNSqYNonVeHvpDLPQRq+sQQYhZr1btjevf71WIqEcez0DFPWrRNIkkFCx6yLLYVdfb90O2662H/4pZrshwI6xJTiNKWs+ld1SbWXlvFjwiLb1dBhKLtrnfDI6yhVexnQxWKOgZF8vl7t6OnzQjMcirnubePnrYHqhKjtBTtf7+N4R/6FCpv70Q/GhzzjvChV4xuAM8LFechOGn7Vi9QpTG1D9mmk5DGBXp5O3F/+BQO4ZxfpTN7cRVxUOL7OMgtRZ4glttdYeK7zMAcgSXsab+TgivJ+Vi3n23S2ZpzoNTFUyNorZF/8u0R3wfIu0O0N1RHZlhWVALlVszlDefjMlwPTSk62FGUL6cOy7+i+ZyG6X1OnuAw4xqZZZR43zVZ3d/Pf4v9DlfceMV7NmKd5amkHm6OVE14X56ciG4+Dw6Oi4g2TP1eV60jY0Qo7esJ0OZ63wTJf2QXeqrkKOlBufD7aAClDgsdJ9MQxkl/GBOkOKV0ElzvZtHAcHoUGTs/FE0kSxa3p7ucVHzCazeHSXW1FNOliFaPqKYVYBYNfCo10ytDzKZmE4E9VBNme0Qh3WTDW6EMafKE9DyZZEXw7Pfv+QTDZCsjgK5dsDw/vV70TbHBB/SPt7cZVks3invca7AapYs37hmiKfgkLSmPIpPxiv8JhMgWUCdpRG7413mZ63vwX4Ba+Q3YWWKE3v4VCfhEVwch2zU9+JSNtAQ54wOxvuHUb8TPUCcJEbywTh7LKN/3oKIh10dSHVo+pPJ7aZCYUWZ/ea1+kfHE+nNDNVE/7CVWeAhwzIpOFRfva6vWN0RVsCw15V2PxNvl3pAOONwtKvtpVZtWCtVI79EF10dwbYl66lnn5NA7QfeeWgFqjWTHmCYpvG8AegXwww/AjD4AzH2i+N4aVRj+39tgxU31nPVvOIiWuv7Z+ZeqHaDjuLqEJDf23uxqYY+7FOe/qNhsvWE30g3pCBxtnr2/CJmPhbCywySTR4NFoubVv5m5IjOWjYjqHTHWgeVcXf2vhxne2Xm2SWXgUndWrRHsbwIzrWTt+ARnZlFnieOp01ha0S1PfLfMqu0S9rJQoUPVdm5u10taEc44LpHe9Lj0hSokhSwqvR0SfZTDCJJ3dKjm2sNXTBAX1WhE7ktbLhTuf1ZgUn5BTq4EeBGVnJ49cQa2ceRBd6+bqTPSzWW5w65opuILBLBgzmK4jYS9z6VC130BOqCMa3gLZZwXIqPUTAVfOX9Vzz2VpKYbimtX/tFN93rlX5EkJx7XgyETRdub2HkNF5bGykOY2Huko1QynAZi+JOPN050Jd2c1VIL");
    }

    //获取唯一可用的对象
    public static SpireDocUtils getInstance() {
        return instance;
    }





    public static String Word2Pdf(String filePath, String outPath) {

        //加载word示例文档
        Document document = new Document();
        document.loadFromFile(filePath);
        //保存这
        document.saveToFile(outPath, FileFormat.PDF);
        return outPath;

    }



    public static String MultipleMergeFiles1(List<String> filePaths, String path) {


        if (filePaths.size() == 0) {
            return "";
        } else if (filePaths.size() == 1) {
            return filePaths.get(0);
        }

        for (int i = 0; i < filePaths.size() - 1; i++) {
            //加载第一个文档

            Document document = new Document(filePaths.get(i));

            //使用insertTextFromFile方法将第二个文档的内容插入到第一个文档

            document.insertTextFromFile(filePaths.get(i + 1), FileFormat.Docx_2013);

            //保存文档
            if (i == filePaths.size() - 2) {
                document.saveToFile(path, FileFormat.Docx_2013);
            } else {
                document.saveToFile(filePaths.get(i + 1), FileFormat.Docx_2013);
            }

        }

        return path;

    }


    public static String MultipleMergeFiles2(List<String> filePaths, String path) {

        for (int i = 0; i < filePaths.size() - 1; i++) {
            MergeFiles2(filePaths.get(i), filePaths.get(i + 1), path);
            //保存文档
            if (i == filePaths.size() - 2) {

            } else {
                filePaths.set(i + 1, path);
            }
        }


        return path;
    }


    public static String MergeFiles1(String filePath1, String filePath2, String path) {

        //加载第一个文档

        Document document = new Document(filePath1);

        //使用insertTextFromFile方法将第二个文档的内容插入到第一个文档

        document.insertTextFromFile(filePath2, FileFormat.Docx_2013);

        //保存文档

        document.saveToFile(path, FileFormat.Docx_2013);

        return path;

    }




    /**
     * @param document1
     * @param document2
     * @return
     */
    public static Document MergeFiles2(Document document1, Document document2) {
        long startTime = System.currentTimeMillis();
        System.out.println("开始合并文件");
        //添加一个section
        document1.addSection();
        //获取第一个文档的最后一个section
        Section lastSection = document1.getLastSection();

        //将第二个文档的段落作为新的段落添加到第一个文档的最后一个section

        //遍历文档2的所有段落内容，添加到文档1

        for (Section section : (Iterable<Section>) document2.getSections()) {

            for (DocumentObject obj : (Iterable<DocumentObject>) section.getBody().getChildObjects()

            ) {

                lastSection.getBody().getChildObjects().add(obj.deepClone());

            }

        }
        System.out.println("结束合并文件： " + (System.currentTimeMillis() - startTime) + " ms.");
        return document1;

    }






    /**
     * @param filePath1
     * @param filePath2
     * @param path
     * @return
     */
    public static String MergeFiles2(String filePath1, String filePath2, String path) {

        //加载第一个文档

        Document document1 = new Document(filePath1);

        //加载第二个文档
        System.out.println("文件：" + filePath2);
        long startTime = System.currentTimeMillis();
        Document document2 = new Document(filePath2);
        System.out.println("读取word时间： " + (System.currentTimeMillis() - startTime) + " ms.");
        //获取第一个文档的最后一个section

        Section lastSection = document1.getLastSection();

        //将第二个文档的段落作为新的段落添加到第一个文档的最后一个section

        //遍历文档2的所有段落内容，添加到文档1

        for (Section section : (Iterable<Section>) document2.getSections()) {

            for (DocumentObject obj : (Iterable<DocumentObject>) section.getBody().getChildObjects()

            ) {

                lastSection.getBody().getChildObjects().add(obj.deepClone());

            }

        }

        //保存文档
        document1.saveToFile(path, FileFormat.Docx_2013);

        return path;


    }



    public static String creatReportDocx(String title, String content, int level, String outPath) {
        //创建Document对象
        Document doc = new Document();
        //添加节
        Section section = doc.addSection();
        Paragraph titleParagraph = null;
        if (title != null) {
            //添加第一个段落
            titleParagraph = section.addParagraph();
            //设置段落文本
            titleParagraph.setText(title);
        }

        //添加第二个段落
        Paragraph paragraph2 = section.addParagraph();
        paragraph2.setText(content);


        if (level == 1) {
            if (titleParagraph != null) {
                //给第一个段落设置样式
                ParagraphStyle style = new ParagraphStyle(doc);
                style.setName("titleStyle");
                style.getCharacterFormat().setBold(true);
                style.getCharacterFormat().setTextColor(Color.BLACK);
                style.getCharacterFormat().setFontName("楷体");
                style.getCharacterFormat().setFontSize(24f);
                doc.getStyles().add(style);
                titleParagraph.applyStyle("titleStyle");
                //给第一个段落
                titleParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            }


            ParagraphStyle style = new ParagraphStyle(doc);
            style.setName("contentStyle");
            style.getCharacterFormat().setFontName("楷体");
            style.getCharacterFormat().setFontSize(14f);
            doc.getStyles().add(style);
            paragraph2.applyStyle("contentStyle");
            //设置首行缩进
            paragraph2.getFormat().setFirstLineIndent(40);


        } else if (level == 2) {
            if (titleParagraph != null) {
                //给第一个段落设置样式
                ParagraphStyle style = new ParagraphStyle(doc);
                style.setName("titleStyle");
                style.getCharacterFormat().setBold(true);
                style.getCharacterFormat().setTextColor(Color.BLACK);
                style.getCharacterFormat().setFontName("楷体");
                style.getCharacterFormat().setFontSize(14f);
                doc.getStyles().add(style);
                titleParagraph.applyStyle("titleStyle");
            }
            //给第一个段落设置样式
            ParagraphStyle style = new ParagraphStyle(doc);
            style.setName("contentStyle");
            style.getCharacterFormat().setFontName("楷体");
            style.getCharacterFormat().setFontSize(14f);
            doc.getStyles().add(style);
            paragraph2.applyStyle("contentStyle");
            //设置首行缩进
            paragraph2.getFormat().setFirstLineIndent(40);


        } else if (level == 3) {
            if (titleParagraph != null) {
                //给第一个段落设置样式
                ParagraphStyle style = new ParagraphStyle(doc);
                style.setName("titleStyle");
                style.getCharacterFormat().setBold(true);
                style.getCharacterFormat().setTextColor(Color.BLACK);
                style.getCharacterFormat().setFontName("楷体");
                style.getCharacterFormat().setFontSize(14f);
                doc.getStyles().add(style);
                titleParagraph.applyStyle("titleStyle");
            }

            //给第一个段落设置样式
            ParagraphStyle style = new ParagraphStyle(doc);
            style.setName("contentStyle");
            style.getCharacterFormat().setFontName("楷体");
            style.getCharacterFormat().setFontSize(14f);
            doc.getStyles().add(style);
            paragraph2.applyStyle("contentStyle");
            //设置首行缩进
            paragraph2.getFormat().setFirstLineIndent(40);
        }

        if (titleParagraph != null) {
            //设置第一个段落的段后间距
            titleParagraph.getFormat().setAfterSpacing(15f);
        }
        //文件名
        String fileName = UUID.randomUUID().toString();

        //保存
        doc.saveToFile(outPath + fileName + ".docx", FileFormat.Docx_2013);

        return outPath + fileName + ".docx";
    }


    public static String CreateWordDocument(String outPath) {
        long startTime = System.currentTimeMillis();
        //创建Word文档
        Document document = new Document();
        //添加一个section
        Section section = document.addSection();

        //添加一个段落至section
        Paragraph para1 = section.addParagraph();
        para1.appendText("");

        //保存文件名
        String fileName = UUID.randomUUID().toString();
        //保存
        document.saveToFile(outPath + fileName + ".docx", FileFormat.Docx_2013);
        System.out.println("生成空word " + (System.currentTimeMillis() - startTime) + " ms.");
        return outPath + fileName + ".docx";

    }

}
